﻿using Microsoft.AspNetCore.Mvc;
using MVC系统mvc.Dto.Process;
using MVC系统mvc.Dto.Purch;
using Newtonsoft.Json;
using WMS_MAV;

namespace MVC系统mvc.Controllers
{
    public class PurchController : Controller
    {
        private readonly IConfiguration _configuration;

        public PurchController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        /// <summary>
        /// 新增采购信息视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddPurch()
        {
            return View();
        }
        /// <summary>
        /// 显示采购信息视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowPurch()
        {
            return View();
        }
        /// <summary>
        /// 显示采购信息视图(入库出库)
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowPurchIn()
        {
            return View();
        }
        /// <summary>
        /// 编辑采购信息视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdatePurch()
        {
            return View();
        }
        /// <summary>
        /// 查看待采购页面
        /// </summary>
        /// <returns></returns>
        public IActionResult DailyPurch()
        {
            return View();
        }
        /// <summary>
        /// 查看已采购页面
        /// </summary>
        /// <returns></returns>
        public IActionResult YiPurch()
        {
            return View();
        }
        /// <summary>
        /// 供应商界面
        /// </summary>
        /// <returns></returns>
        public IActionResult ListSup()
        {
            return View();
        }
        /// <summary>
        /// 物流公司界面
        /// </summary>
        /// <returns></returns>
        public IActionResult ListLogShow()
        {
            return View();
        }
        /// <summary>
        /// 查看待入库页面
        /// </summary>
        /// <returns></returns>
        public IActionResult RuPurchs()
        {
            return View();
        }
        /// <summary>
        /// 查看已入库页面
        /// </summary>
        /// <returns></returns>
        public IActionResult KuYiPurch()
        {
            return View();
        }
        /// <summary>
        /// 添加采购信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetPurchAdd(CreatePurchs field, string token)
        {
            //http://localhost:59256 _configuration["Url"]

            HttpClientWrapper client = new HttpClientWrapper(token);
            //string url = "http://localhost:59256/api/Pruch/CreatePurch";
            string url = _configuration["Url"]+ "/purech/api/Pruch/CreatePurch";
            string list = JsonConvert.SerializeObject(field);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 采购显示(入库出库)
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> PInfoList(ListPurchInDto model)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);

            //string url = "http://localhost:59256/api/Pruch/GetInList?PurchaseCode=" + model.PurchaseCode + "&CreatedDate=" + model.CreatedDate +
            //    "&InventoryState=" + model.InventoryState + "&PageIndex=" + model.PageIndex + "&PageSize=" + model.PageSize;
            string url = _configuration["Url"] + "/purech/api/Pruch/GetInList?PurchaseCode=" + model.PurchaseCode + "&CreatedDate=" + model.CreatedDate +
                "&InventoryState=" + model.InventoryState + "&PageIndex=" + model.PageIndex + "&PageSize=" + model.PageSize;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 采购显示
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> PurchsList(ListPurch model)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);

            //string url = "http://localhost:59256/api/Pruch/GetPurchList?PurchaseCode="+model.PurchaseCode+"&PurchaseDate="+model.PurchaseDate+
            //"&PurchaseSart="+model.PurchaseSart+"&PageIndex="+model.PageIndex+"&PageSize="+model.PageSize;
            string url = _configuration["Url"] + "/purech/api/Pruch/GetPurchList?PurchaseCode=" + model.PurchaseCode+"&PurchaseDate="+model.PurchaseDate+
            "&PurchaseSart="+model.PurchaseSart+"&PageIndex="+model.PageIndex+"&PageSize="+model.PageSize;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 供应商显示
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> SupList(string token)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);

            //string url = "http://localhost:59256/api/Pruch/GetSuppList";
            string url = _configuration["Url"] + "/purech/api/Pruch/GetSuppList";
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 根据采购ID显示供应商
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> SupListPurch(int PurchaseID,string token)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);

            //string url = "http://localhost:59256/api/Pruch/Handles?PurchaseID="+ PurchaseID;
           string url = _configuration["Url"] + "/purech/api/Pruch/Handles?PurchaseID=" + PurchaseID;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 根据采购ID，供应商ID显示物流
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> LogList(int PurchaseID,int SupplierID, string token)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);

            //string url = "http://localhost:59256/api/Pruch/GetLogisticsList?SupplierID="+SupplierID+"&PurchaseID="+PurchaseID;
            string url = _configuration["Url"] + "/purech/api/Pruch/GetLogisticsList?SupplierID=" + SupplierID + "&PurchaseID=" + PurchaseID;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 修改物流信息(提醒发货)
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> UpdateLog(UpdateLogDto model)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);

            // string url = "http://localhost:59256/api/Pruch/UpdateLogistics?LogisticsId="+model.LogisticsId;
            string url = _configuration["Url"] + "/purech/api/Pruch/UpdateLogistics?LogisticsId=" + model.LogisticsId;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.PostAsync(url, model.LogisticsId.ToString());
            return Json(response);

        }
        /// <summary>
        /// 选择供应商
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetUpdateSups(SupUpdsateDto field,string token)
        {
           

            HttpClientWrapper client = new HttpClientWrapper(token);
          //string url = "http://localhost:59256/api/Pruch/UpSupplier?SupplierID="+field.SupplierID+"&PurchaseID="+field.PurchaseID;
            string url = _configuration["Url"] + "/purech/api/Pruch/UpSupplier?SupplierID=" + field.SupplierID + "&PurchaseID=" + field.PurchaseID;
            string list = JsonConvert.SerializeObject(field);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 删除采购信息
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> PurchDelete(DeletePurch model)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);

            //string url = "http://localhost:59256/api/Pruch/DeletePurch?PurchaseID=" + model.PurchaseID;
            string url = _configuration["Url"] + "/purech/api/Pruch/DeletePurch?PurchaseID=" + model.PurchaseID;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.PostAsync(url, model.PurchaseID.ToString());
            return Json(response);

        }
        /// <summary>
        /// 采购信息反填
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> FabPurch(string token, int PurchaseID)
        {
            

            HttpClientWrapper client = new HttpClientWrapper(token);
           // string url = "http://localhost:59256/api/Pruch/HandlePurch?PurchaseID=" + PurchaseID;
            string url = _configuration["Url"] + "/purech/api/Pruch/HandlePurch?PurchaseID=" + PurchaseID;
            var response = await client.GetAsync(url);
            return Json(response);
        }
       
        /// <summary>
        /// 采购信息修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetUpdatePruch(UpdatePurchDto field, string token)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(token);
            //string url = "http://localhost:59256/api/Pruch/UpdatePurch";
            string url = _configuration["Url"] + "/purech/api/Pruch/UpdatePurch";
            string list = JsonConvert.SerializeObject(field);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 根据产品编号显示仓库信息
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> PurchIngs(ListPurch model)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);

            //string url = "http://localhost:59256/api/Pruch/GetPurchList?PurchaseCode="+model.PurchaseCode+"&PurchaseDate="+model.PurchaseDate+
            //"&PurchaseSart="+model.PurchaseSart+"&PageIndex="+model.PageIndex+"&PageSize="+model.PageSize;
            string url = _configuration["Url"] + "/purech/api/Pruch/GetPurchList?PurchaseCode=" + model.PurchaseCode + "&PurchaseDate=" + model.PurchaseDate +
            "&PurchaseSart=" + model.PurchaseSart + "&PageIndex=" + model.PageIndex + "&PageSize=" + model.PageSize;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 根据产品编号显示入库信息
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> PurchsListIng(string FinishedProductNo,string token)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            
            //string url = "http://localhost:15950/api/Inventory/ShowGetCode?FinishedProductNo=" + FinishedProductNo;
           string url = _configuration["Url"] + "ckgl/api/Inventory/ShowGetCode?FinishedProductNo=" + FinishedProductNo;
            //var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
    }
}
